
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories.Internal;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Repositories
{
    [AutoDi()]//注意这里 自动DI
    public class YH_ScreeningPlanRepository : BaseDBRepository<YH_ScreeningPlan>
    //统一继承BaseDBRepository并传入`Entity`泛型参数
    {
        public YH_ScreeningPlanRepository(CoreDBContext dbContext) : base(dbContext)
        {
        }
        //public override IQueryable<YH_ScreeningPlan> QueryBase()
        //{
        //    return base.QueryBase()
        //             .Include(a => a.SysUser)
        //        .Include(a => a.SysDepartment)
        //        .Include(a => a.YH_ScreeningPlanProject).ThenInclude(a => a.YH_ScreeningPlanProDetail).ThenInclude(a => a.YH_Grade)
        //        .Include(a => a.YH_ScreeningPlanProject).ThenInclude(a => a.SysArea)
        //        .Include(a => a.YH_ScreeningPlanUser);
        //}

        /// <summary>
        /// UPDATE
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateForPCAsync(YH_ScreeningPlan entity)
        {
            //using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            //{
            //获取提醒表
            var YH_ScreeningPlanTask = await _dbContext.Set<YH_ScreeningPlanTask>().Where(i => i.ScreeningPlanId == entity.Id).ToListAsync();
            //根据提醒表删除提醒人员
            YH_ScreeningPlanTask.ForEach(p =>
            {
                var YH_ScreeningPlanWarn = _dbContext.Set<YH_ScreeningPlanWarn>().Where(dpd => dpd.ScreeningPlanTaskId == p.Id).ToList();
                YH_ScreeningPlanWarn.ToList().ForEach(it => _dbContext.Set<YH_ScreeningPlanWarn>().Remove(it));
            });

            //删除提醒表
            YH_ScreeningPlanTask.ForEach(it => _dbContext.Set<YH_ScreeningPlanTask>().Remove(it));

            //获取参与人
            var YH_ScreeningPlanUser = await _dbContext.Set<YH_ScreeningPlanUser>().Where(i => i.ScreeningPlanId == entity.Id).ToListAsync();

            //获取排查项目
            var YH_ScreeningPlanProject = await _dbContext.Set<YH_ScreeningPlanProject>().Where(i => i.ScreeningPlanId == entity.Id).ToListAsync();

            //根据排查项目删除排查标准
            YH_ScreeningPlanProject.ForEach(p =>
            {
                var YH_ScreeningPlanProDetail = _dbContext.Set<YH_ScreeningPlanProDetail>().Where(dpd => dpd.ScreeningPlanProjectId == p.Id).ToList();
                YH_ScreeningPlanProDetail.ToList().ForEach(it => _dbContext.Set<YH_ScreeningPlanProDetail>().Remove(it));
            });

            //删除排查项目
            YH_ScreeningPlanProject.ForEach(it => _dbContext.Set<YH_ScreeningPlanProject>().Remove(it));



            //删除参与人
            YH_ScreeningPlanUser.ForEach(it => _dbContext.Set<YH_ScreeningPlanUser>().Remove(it));

            //先把删除的东西保存
            //await _dbContext.SaveChangesAsync();

            //修改排查内容主表
            var YH_ClearancePlan = await _dbContext.Set<YH_ScreeningPlan>().Where(i => i.Id == entity.Id).FirstOrDefaultAsync();
            EntityToEntity(entity, YH_ClearancePlan, new List<string>() { "YH_ScreeningPlanUser", "YH_ScreeningPlanTask", "YH_ScreeningPlanProject", "SysDepartment", "SysUser" });

           

            //插入新的实体
            entity.YH_ScreeningPlanTask.ToList().ForEach(cpp => { cpp.ScreeningPlanId = entity.Id; });
            await _dbContext.Set<YH_ScreeningPlanTask>().AddRangeAsync(entity.YH_ScreeningPlanTask);

            entity.YH_ScreeningPlanUser.ToList().ForEach(cpp => { cpp.ScreeningPlanId = entity.Id; });
            await _dbContext.Set<YH_ScreeningPlanUser>().AddRangeAsync(entity.YH_ScreeningPlanUser);

            entity.YH_ScreeningPlanProject.ToList().ForEach(cpp => { cpp.ScreeningPlanId = entity.Id; });
            await _dbContext.Set<YH_ScreeningPlanProject>().AddRangeAsync(entity.YH_ScreeningPlanProject);
            //提交新的修改
            await _dbContext.SaveChangesAsync();
            //    scope.Complete();
            //}
            return true;
        }
    }
}
